package {
	
	/*
	
	 Copyright (c) 2010 Ben Caulfield

	 Permission is hereby granted, free of charge, to any person
	 obtaining a copy of this software and associated documentation
	 files (the "Software"), to deal in the Software without
	 restriction, including without limitation the rights to use,
	 copy, modify, merge, publish, distribute, sublicense, and/or sell
	 copies of the Software, and to permit persons to whom the
	 Software is furnished to do so, subject to the following
	 conditions:
	
	 The above copyright notice and this permission notice shall be
	 included in all copies or substantial portions of the Software.
	
	 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	 OTHER DEALINGS IN THE SOFTWARE.

	*/
	
	import flash.events.Event;
	import flash.events.ContextMenuEvent;
	
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import flash.net.URLRequest;
	import flash.net.navigateToURL;

	import flash.display.*;
	  
	import org.keeners.imageviewer.Config;
	import org.keeners.imageviewer.Controller;
	
	public class ImageViewer extends MovieClip {
	
		public var config:Config;
		public var controller:Controller;
		
		public function ImageViewer () { 
			
			// set up the context menu
			var project_home:String = 'http://code.google.com/p/keenerview/';
			var project_name:String = 'About keenerview v0.9.2';			
			var project_notice:ContextMenuItem = new ContextMenuItem(project_name);
			project_notice.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, function(e:ContextMenuEvent){navigateToURL(new URLRequest(project_home));});			
			var cmenu:ContextMenu = new ContextMenu();
			cmenu.customItems.push(project_notice)
			cmenu.hideBuiltInItems();
			contextMenu = cmenu;
			
			// YUK!! http://code.google.com/p/swfobject/issues/detail?id=3
			// required for dynamic embed
			stage.scaleMode = StageScaleMode.NO_SCALE;
 			stage.align = StageAlign.TOP_LEFT; 			
			stage.addEventListener(Event.RESIZE, resize_handler);
			stage.dispatchEvent(new Event(Event.RESIZE)); // force stage resize event for normal cases			 
		}
		
		public function start(){			
			var params = root.loaderInfo.parameters
			var _stage:Stage = stage;				
			config = new Config();
			controller = new Controller(_stage, config, params);
			controller.initialize_image();
		}
		
		function resize_handler(event:Event):void {
			if (stage.stageHeight > 0 && stage.stageWidth > 0) {
				stage.removeEventListener(Event.RESIZE, resize_handler); // only execute once			
				start();
		  	}
		}
		
	}
}
